package com.yuexun.beilunpatient.ui.personaldata.ui;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.yuexun.beilunpatient.R;
import com.yuexun.beilunpatient.base.BaseKJActivity;
import com.yuexun.beilunpatient.config.AppConfig;
import com.yuexun.beilunpatient.ui.bean.BaseListEntity;
import com.yuexun.beilunpatient.ui.personaldata.bean.PersonalInfoBean;
import com.yuexun.beilunpatient.ui.personaldata.bean.PersonalInfoData;
import com.yuexun.beilunpatient.ui.personaldata.model.impl.PersonalDataModel;
import com.yuexun.beilunpatient.ui.personaldata.presenter.impl.PersonalDataPresenter;
import com.yuexun.beilunpatient.ui.personaldata.ui.view.IPersonalDataView;
import com.yuexun.beilunpatient.utils.AgeUtil;
import com.yuexun.beilunpatient.widget.LoadingDialog;

import org.kymjs.kjframe.utils.PreferenceHelper;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by chenss on 2019/5/16.
 */
public class Act_PersonalData extends BaseKJActivity implements IPersonalDataView {

    @Bind(R.id.tv_name)
    TextView tvName;
    @Bind(R.id.tv_sex)
    TextView tvSex;
    @Bind(R.id.tv_age)
    TextView tvAge;
    @Bind(R.id.tv_idcard)
    TextView tvIdcard;
    @Bind(R.id.tv_phone)
    TextView tvPhone;
    @Bind(R.id.tv_permanent_type)
    TextView tvPermanentType;
    @Bind(R.id.tv_nation)
    TextView tvNation;
    @Bind(R.id.tv_birth)
    TextView tvBirth;
    @Bind(R.id.tv_education)
    TextView tvEducation;
    @Bind(R.id.tv_marriage)
    TextView tvMarriage;
    @Bind(R.id.tv_work)
    TextView tvWork;
    @Bind(R.id.tv_occupation)
    TextView tvOccupation;
    @Bind(R.id.tv_residential_address)
    TextView tvResidentialAddress;
    @Bind(R.id.tv_permanent_address)
    TextView tvPermanentAddress;
    @Bind(R.id.tv_contact_name)
    TextView tvContactName;
    @Bind(R.id.tv_contact_phone)
    TextView tvContactPhone;
    @Bind(R.id.tv_blood)
    TextView tvBlood;
    @Bind(R.id.tv_blood_RH)
    TextView tvBloodRH;

    PersonalDataPresenter presenter;
    LoadingDialog dialog;

    @Override
    public void setRootView() {
        super.setRootView();
        setContentView(R.layout.act_personaldata);
        ButterKnife.bind(this);
    }

    @Override
    public void initData() {
        super.initData();
        presenter = new PersonalDataPresenter(new PersonalDataModel(), this);
        dialog = new LoadingDialog(aty);
        dialog.show();

        Map<String, String> params = new HashMap<>();
        params.put("sessionUuidPatient", PreferenceHelper.readString(aty, AppConfig.USER_INFO, AppConfig.SESSION_ID));
        presenter.inquirePatientInfoByRuiRuanNew(params);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick({R.id.title_back})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.title_back:
                finish();
                break;
        }
    }

    @Override
    public void getPatientInfoByRuiRuanNew(BaseListEntity<PersonalInfoBean> entity) {
        dialog.dismiss();
        if(entity.getStatus().equals("200")){
            if(entity.getDatas().get(0).getCode() == 1){
                PersonalInfoData infoData = entity.getDatas().get(0).getRows().get(0);
                tvName.setText(infoData.getName());
                tvSex.setText(infoData.getGender());
                try {
                    tvAge.setText(AgeUtil.getAge(new SimpleDateFormat("yyyy-MM-dd").parse(infoData.getBirthday())) + "岁");
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                tvIdcard.setText(infoData.getPersonid());
                tvPhone.setText(infoData.getMobilephone());
                tvPermanentType.setText(infoData.getResidencemark());
                tvNation.setText(infoData.getNationname());
                tvBirth.setText(infoData.getBirthday());
                tvEducation.setText(infoData.getEducation());
                tvMarriage.setText(infoData.getMarriage());
                tvWork.setText(infoData.getEmployername());
                tvOccupation.setText(infoData.getOccupation());
                tvResidentialAddress.setText(infoData.getHomeaddress());
                tvPermanentAddress.setText(infoData.getRegisteredadd());
                tvContactName.setText(infoData.getContectname());
                tvContactPhone.setText(infoData.getContecttel());
                tvBlood.setText(infoData.getAbocode());
                tvBloodRH.setText(infoData.getRhcode());
            }
        }
    }
}
